package com.future.baselib.utils;

import android.content.Context;
import android.text.Layout;
import android.text.TextPaint;
import android.text.TextUtils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 周大虎 on 2017/3/6.
 */

public class StringUtils {


    /**
     * 获取用户随机码
     *
     * @return
     */
    public static String getUserRandomString() {
        String data = DateUtils.getDateYMDHMS();
        if (TextUtils.isEmpty(data)) {
            data = getRandomString(8);
        }
        return getRandomString(2).concat(data.substring(3, data.length())).concat(getRandomString(3));
    }

    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomNum(int length) { //length表示生成字符串的长度
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }


    /**
     * 邮箱格式是否正确
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {

        if (TextUtils.isEmpty(email))
            return false;

        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";

        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(email);

        return matcher.matches();

    }


    /**
     * 验证手机号码
     *
     * @param mobiles
     * @return boolean
     */
    public static boolean isMobilePhone(String mobiles) {
        boolean flag = false;
        if (TextUtils.isEmpty(mobiles))
            return flag;
        if (mobiles.length() != 11)
            return flag;
        try {
            Pattern p = Pattern.compile("^[1][3,4,5,7,8,9][0-9]{9}$");
            Matcher m = p.matcher(mobiles);
            flag = m.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 随机获取上传文件的  图片 文件名
     */
    public static String getUploadQiNiuNameForImg() {
        return "android_img".concat(DateUtils.getDateYMDHMS()).concat(getRandomString(5).concat(".jpg"));
    }

    /**
     * 随机获取上传文件的文件名
     */
    public static String getUploadQiNiuName() {
        return "android_file".concat(DateUtils.getDateYMDHMS()).concat(getRandomString(10));
    }

    /**
     * 随机获取上传文件的  语音 文件名
     */
    public static String getUploadQiNiuNameForVoice() {
        return "android_voice".concat(DateUtils.getDateYMDHMS()).concat(getRandomString(5).concat(".mp3"));
    }


    /**
     * 随机获取上传文件的  视频 文件名
     */
    public static String getUploadQiNiuNameForVideo() {
        return "android_video".concat(DateUtils.getDateYMDHMS()).concat(getRandomString(5).concat(".mp4"));
    }


    public static Double parseDouble(String string) {
        double proportion;
        try {
            proportion = Double.parseDouble(string);
        } catch (NumberFormatException ex) {
            proportion = 0;
        }
        return proportion;
    }

    public static int count(String text, String sub) {
        int count = 0, start = 0;
        while ((start = text.indexOf(sub, start)) >= 0) {
            start += sub.length();
            count++;
        }
        return count;
    }


    public static int count(String text, String sub, int line) {
        int count = 0, start = 0;
        while ((start = text.indexOf(sub, start)) >= 0) {
            start += sub.length();
            count++;
            if (count == line)
                return start;
        }
        return start;
    }


    public static float getTextWidth(Context context, String text, float textSize) {
        TextPaint paint = new TextPaint();
        float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
        paint.setTextSize(scaledDensity * textSize);
        return (float) Math.ceil(Layout.getDesiredWidth(text, paint));
    }

    /**
     * 是否为中文字符
     *
     * @param c
     * @return
     */
    public static final boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    /**
     * 用户名长度
     */
    private static final int STANDARD_USERNAME_LENGTH = 12;

    /**
     * @return 用户名长度是否小于6位中文，12位英文
     */
    public static boolean isUserNameStandard(String input) {
        char[] inputArray = input.toCharArray();
        int realLength = 0;
        for (int i = 0; i < inputArray.length; i++) {
            if (isChinese(inputArray[i])) {
                realLength += 2;
            } else {
                realLength++;
            }
        }
        if (realLength <= STANDARD_USERNAME_LENGTH) {
            return true;
        } else {
            return false;
        }
    }


}
